<?php
class Cp_SettingsController extends Pidguin_Controller_Cp_Action
{	
	public function indexAction()
	{
		
	}
	
	public function siteAction()
	{
		$form = new Application_Form_Cp_Settings_Site();
		$this->view->form = $form;
	}
	
	public function navAction()
	{
		$model = Model_Menu::getInstance();
		$this->view->items = $model->getItems();
	}
	
	public function navSaveAction()
	{
		$this->_disableView();
		if (!$this->isAjax()) {
			throw new Zend_Controller_Action_Exception('not ajax');
		}
		if (!$this->_getParam('items')) {
			throw new Zend_Controller_Action_Exception('Empty params');
		}
		Model_Menu::getInstance()->getAdapter()->query('TRUNCATE TABLE `menu`');
		foreach ($this->_getParam('items') as $item) {
			if (!preg_match('/^mcid([0-9]*)$/i', $item['id'], $m)) {
				throw new Zend_Controller_Action_Exception('Incorrect item id');
			}
			$parent_id = 0;
			if ($item['parent_id'] != '0' && !preg_match('/^mcid([0-9]*)$/i', $item['parent_id'], $parent_id)) {
				throw new Zend_Controller_Action_Exception('Incorrect item id');
			}
			$r = Model_Menu::getInstance()->createRow();
			$r->id = $m[1];
			$r->label = $item['label'];
			$r->uri = $item['uri'];
			$r->parent_id = $parent_id;
			$r->enabled = 1;
			$r->save();
		}
	}
}
